﻿using System;
using System.Xml;

namespace Jayden.Dll.Sc2.Descriptions
{
	public class Description : IComparable<Description>
	{
		private string m_Id;
		private string m_Name;

		protected Description(string id, string name = null) { m_Id = id; m_Name = name; }
		public Description(XmlNode node)
		{
			m_Id = node.Attributes["id"].Value;
			XmlAttribute name = node.Attributes["name"];
			if (name != null)
				m_Name = name.Value;
		}

		private string NameFromId
		{
			get
			{
				string result = string.Empty;
				for (int c = 0; c < m_Id.Length; c++)
				{
					if ((char.IsUpper(m_Id[c]) && c > 0 && !char.IsUpper(m_Id[c - 1])) || (char.IsNumber(m_Id[c]) && c > 0 && !char.IsNumber(m_Id[c - 1])))
						result += " ";
					result += m_Id[c];
				}
				return result;
			}
		}

		public string Id { get { return m_Id; } }
		public virtual string Name { get { return m_Name ?? NameFromId; } }

		public override bool Equals(object obj)
		{
			if (obj is Description)
				return Id.Equals(((Description)obj).Id);
			return false;
		}

		private int? m_HashCode = null;
		public override int GetHashCode()
		{
			if (!m_HashCode.HasValue)
				m_HashCode = Id.GetHashCode();
			return m_HashCode.Value;
		}
		public override string ToString() { return Id; }

		public int CompareTo(Description other) { return Id.CompareTo(other.Id); }
	}
}
